<template>
  <div class="random-preview-wrapper">
    <div class="info-wrap">
      <div class="tag-item">【题型】：{{detailData.questionType | formatType}}</div>
      <div class="tag-item">【编号】：{{detailData.id}}</div>
      <div class="tag-item">【难度】：{{detailData.difficulty | formatDifficulty}}</div>
      <div class="tag-item">【标签】：{{detailData.tags}}</div>
      <div class="tag-item">【学科】：{{detailData.subjectName}}</div>
      <div class="tag-item">【目录】：{{detailData.directoryName}}</div>
      <div class="tag-item">【方向】：{{detailData.direction}}</div>
    </div>
    <hr />
    <div class="topic-wrap">
      <div class="info-wrap">
        <div class="tag-item">【题干】：</div>
      </div>
      <div class="content">
        <div class="question" v-html="detailData.question"></div>
        <div class="options-wrap">
          <div v-if="detailData.questionType === '1'">
            <div style="margin-bottom: 10px;">单选题 选项：（以下选中的选项为正确答案）</div>
            <div class="padd radio-item" v-for="(item,index) in detailData.options" :key="index">
              <el-radio v-model="answerRadio" :label="item.isRight">{{item.title}}</el-radio>
            </div>
          </div>
          <div v-if="detailData.questionType === '2'">
            <div class="padd">多选题 选项：（以下选中的选项为正确答案）</div>
            <div class="padd" v-for="(item, index) in detailData.options" :key="index">
              <!-- <el-checkbox label="复选框 A"></el-checkbox> -->
              <el-checkbox v-model="item.isChecked">{{item.title}}</el-checkbox>
            </div>
          </div>
          <div v-if="detailData.questionType === '3'"></div>
          <div class="shade"></div>
        </div>
      </div>
    </div>
    <hr />
    <div class="answer-wrap">
      <div class="info-wrap">
        <div class="tag-item align">
          【参考答案】：
          <el-button type="danger" size="small" @click="showVideo">视频答案预览</el-button>
        </div>
      </div>
      <video
        ref="video"
        width="400"
        height="300"
        controls
        :src="detailData.videoURL"
        v-if="isShowVideo"
      ></video>
    </div>
    <hr />
    <div class="info-wrap">
      <div class="tag-item align">
        【答案解析】：
        <div v-html="detailData.answer"></div>
      </div>
    </div>
    <hr />
    <div class="info-wrap">
      <div class="tag-item align" style="width: 500px">
        【题目备注】：
        <div>{{detailData.remarks}}</div>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="onClose">关闭</el-button>
    </span>
  </div>
</template>

<script>
import { detail } from "@/api/hmmm/questions.js";
import { simple } from "@/api/hmmm/subjects.js";
export default {
  props: {
    visible: {
      type: Boolean,
    },
    detailId: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      detailData: {},
      simpleList: [],
      isShowVideo: false,
      answerCheckList: [],
      answerRadio: 1,
    };
  },
  async mounted() {
    await this.getSimple();
    await this.getDetail();
  },
  methods: {
    async getDetail() {
      try {
        const res = await detail({ id: this.detailId });
        this.detailData = res.data;
        this.handleOptionsData();
      } catch (error) {
        this.$message.error(error.message);
      }
    },
    async getSimple() {
      try {
        const res = await simple();
        this.simpleList = res.data;
      } catch (error) {
        this.$message.error(error.message);
      }
    },
    async showVideo() {
      if (this.detailData.videoURL) {
        this.isShowVideo = true;
        await this.$nextTick();
        this.$refs.video.play();
      }
    },
    handleOptionsData() {
      const options = this.detailData.options;
      if (options) {
        options.forEach((item) => {
          item.isChecked = Boolean(item.isRight);
        });
      }
    },
    hideVideo() {
      this.isShowVideo = false;
      this.answerCheckList = [];
    },
    onClose() {
      this.hideVideo();
      this.$emit("update:visible", false);
    },
  },
};
</script>

<style lang="less">
.random-preview-wrapper {
  .info-wrap {
    display: flex;
    flex-wrap: wrap;

    .tag-item {
      width: 195px;
      margin: 10px 5px;

      &.align {
        display: flex;
        align-items: center;
        width: 500px;
      }
    }
  }

  .topic-wrap {
    .content {
      padding: 0 20px;

      .options-wrap {
        position: relative;

        .shade {
          position: absolute;
          left: 0;
          top: 0;
          width: 100%;
          height: 100%;
        }
      }

      .question {
        color: #0000ff;
      }

      .padd {
        padding: 5px 0;
      }
    }
  }

  .dialog-footer {
    display: flex;
    justify-content: flex-end;
  }
}
</style>